from rest_framework import serializers

from users.models import Address
from .models import Area

class AreaSerializer(serializers.ModelSerializer):
    # 行政区划信息序列化器
    class Meta:
        model=Area
        fields=('id','name')

class SubAreaSerializer(serializers.ModelSerializer):
        # 下级行政区序列化器
    subs=AreaSerializer(many=True,read_only=True)
    class Meta:
        model=Area
        fields=('id','name','subs')


class AddressSerializer(serializers.ModelSerializer):
    # read_only=True 序列化使用  只读
    province=serializers.StringRelatedField(read_only=True)
    city=serializers.StringRelatedField(read_only=True)
    district=serializers.StringRelatedField(read_only=True)
    # required=True  必须携带参数
    province_id=serializers.IntegerField(label='省ID',required=True)
    city_id=serializers.IntegerField(label='市ID',required=True)
    district_id=serializers.IntegerField(label='区ID',required=True)

    class Meta:
        model=Address
        exclude=('user','is_deleted','create_time','update_time')

    def create(self,validated_data):
        validated_data['user']=self.context['request'].user

        return super().create(validated_data)


class AddressTitleSerializer(serializers.ModelSerializer):
    #地址标题
    class Meta:
        model=Address
        fields=('title',)
